﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Utility;

namespace WebHoiChoMuaBan.Controllers.Helpers
{
    public class FriendHelper : HelperBase
    {
        public List<Member> getAllPendingMembers(int memId)
        {
            List<FriendList> requesting = (from e in this.dataContext.FriendLists where (e.Accept == 'W' && e.MemberRequesting.MemId == memId) select e).ToList<FriendList>();
            List<Member> result = new List<Member>();
            foreach (FriendList tmp in requesting)
            {
                Member mem = (from e in this.dataContext.Members where e.MemId == tmp.MemIdRequested select e).SingleOrDefault();
                result.Add(mem);
            }
            return result;
        }

        public List<Member> getAllApprovedMembers(int memId)
        {
            List<FriendList> requesting = (from e in this.dataContext.FriendLists where (e.Accept == 'A' && e.MemberRequesting.MemId == memId) select e).ToList<FriendList>();
            
            List<FriendList> requested = (from e in this.dataContext.FriendLists where (e.Accept == 'A' && e.MemberRequested.MemId == memId) select e).ToList<FriendList>();
            foreach (FriendList fl in requested)
            {
                requesting.Add(fl);
            }
            List<Member> result = new List<Member>();
            foreach (FriendList tmp in requesting)
            {
                Member mem = (from e in this.dataContext.Members where e.MemId == tmp.MemIdRequested select e).SingleOrDefault();
                if (mem.MemId != memId)
                    result.Add(mem);
                    mem = (from e in this.dataContext.Members where e.MemId == tmp.MemIdRequesting select e).SingleOrDefault();
                if (mem.MemId != memId)
                    result.Add(mem);
            }
            return result;
        }

        public bool SubmitMakeFriend(int requesting, int requested, char c)
        {
            try
            {
                FriendList fList = (from e in this.dataContext.FriendLists where (e.MemIdRequested == requested && e.MemIdRequesting == requesting) select e).SingleOrDefault();
                fList.Accept = c;
                if (c == 'B')
                {
                    BlackList bl = new BlackList();
                    bl.BlackMemId = requested;
                    bl.MemId = requesting;
                    this.dataContext.BlackLists.InsertOnSubmit(bl);
                }
                else
                {
                    if (c == 'R')
                    {
                        this.dataContext.FriendLists.DeleteOnSubmit(fList);
                    }
                }
                this.dataContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public bool FriendRequest(int requestingId, int requestedId)
        {
            FriendList fList = new FriendList();
            fList.MemIdRequested = requestedId;
            fList.MemIdRequesting = requestingId;
            fList.MakeFriendDate = DateTime.Now;
            fList.Accept = 'W';
            this.dataContext.FriendLists.InsertOnSubmit(fList);
            try
            {
                this.dataContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }


        }

    }
}